////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "WNScripting/inc/WNParseTree.h"
#include "WNScripting/inc/WNTopLevelElement.h"
#include "WNMemory/inc/WNAllocation.h"

WNParseTree::WNParseTree(WN_SIZE_T _startLine,
                WN_SIZE_T _endLine,
                WN_SIZE_T _startChar,
                WN_SIZE_T _endChar,
                WNTopLevelElement* _topLevelElement):  
    WNParseNode(_startLine, _endLine, _startChar, _endChar),
    mTopLevelElement(_topLevelElement) {
}

WNParseTree::~WNParseTree() {
    if (mTopLevelElement != WN_NULL) {
        WN_DELETE(mTopLevelElement);
    }

    for (WN_UINT32 i = 0; i < mTopLevelElements.size(); ++i) {
        WN_DELETE(mTopLevelElements[i]);
    }
}

void WNParseTree::CompileCode(WNScriptingEngineData& _scriptingData, WNFunction** _function) {
    mTopLevelElement->OptimizeAndGenerateFunction(_scriptingData, _function);
}

WN_BOOL WNParseTree::ReportErrors() {
    WN_BOOL retVal = WNParseNode::ReportErrors();
    return(retVal & mTopLevelElement->ReportErrors());
}